package cn.sangedon.review.data.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.sangedon.review.common.constant.TypeConstant;
import cn.sangedon.review.common.domain.data.SpiderMateData;
import cn.sangedon.review.common.exception.ServiceException;
import cn.sangedon.review.common.service.data.SpiderMateDataService;
import cn.sangedon.review.common.service.data.SpiderService;
import cn.sangedon.review.common.service.data.StockDayInfoService;
import cn.sangedon.review.common.service.data.StockInfoService;
import cn.sangedon.review.data.spider.impl.DropLimitSpider;
import cn.sangedon.review.data.spider.impl.RiseLimitSpider;
import cn.sangedon.review.data.spider.impl.SnowballQuotationSpider;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@DubboService
public class SpiderServiceImpl implements SpiderService {
    @Autowired
    private SpiderMateDataService spiderMateDataService;

    @Autowired
    private StockDayInfoService stockDayInfoService;

    @Autowired
    private StockInfoService stockInfoService;

    @Autowired
    private RiseLimitSpider riseLimitSpider;

    @Autowired
    private DropLimitSpider dropLimitSpider;

    @Autowired
    private SnowballQuotationSpider stockQuotationSpider;

    @Override
    public List<SpiderMateData> listSpiderMateData() {
        QueryWrapper<SpiderMateData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spider_status", 1);
        return spiderMateDataService.list(queryWrapper);
    }

    @Override
    public void crawLimitData(Integer type, String crawDate) {
        try {
            DateTime dateTime = DateUtil.parse(crawDate, "yyyyMMdd");
            if (TypeConstant.RISE_LIMIT.equals(type)) {
                riseLimitSpider.crawlData(crawDate);
            } else if (TypeConstant.DROP_LIMIT.equals(type)) {
                dropLimitSpider.crawlData(crawDate);
            } else {
                log.info("涨跌停类型设置有误:{}", type);
            }
        } catch (Exception e) {
            throw new ServiceException("获取东方财富涨跌停数据失败:" + e.getLocalizedMessage());
        }
    }

    @Override
    public void crawSnowball() {
        stockQuotationSpider.crawlData();
    }
}
